package com.project.lvshi.controller.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.lvshi.domain.Result;
import com.project.lvshi.domain.SysOrder;
import com.project.lvshi.domain.SysProblem;
import com.project.lvshi.domain.User;
import com.project.lvshi.service.SysOrderService;
import com.project.lvshi.service.SysProblemService;
import com.project.lvshi.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@ResponseBody
@RequestMapping("order")
public class SysOrderController {

    @Autowired
    private SysOrderService sysOrderService;
    @Autowired
    private UserService userService;

    @PostMapping("getSysOrderPage")
    public Result getSysOrderPage(@RequestBody SysOrder sysOrder) {
        Page<SysOrder> page = new Page<>(sysOrder.getPageNumber(),sysOrder.getPageSize());
        QueryWrapper<SysOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(SysOrder::getCreateTime);
        Page<SysOrder> sysOrderPage = sysOrderService.page(page, queryWrapper);
        for (SysOrder order : sysOrderPage.getRecords()) {
            User user = userService.getById(order.getUserId());
            User layer = userService.getById(order.getLayerId());
            order.setLoginAccount(user.getLoginAccount());
            order.setLayerAccount(layer.getLoginAccount());
        }
        return Result.success(sysOrderPage);
    }

    @GetMapping("removeSysOrder")
    public Result removeSysOrder(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                sysOrderService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("id不能为空！");
        }
    }

}
